<template>
  <div class="main">
    <!-- 条件查询 -->
    <el-card shadow="never" class="card">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <!-- 查询表单 -->
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        size="small"
        label-width="80px"
      >
        <el-form-item label="作者">
          <el-input v-model="form.author" />
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="活动区域">
          <el-select v-model="form.editorType" placeholder="编辑类型">
            <el-option label="富文本" :value="0" />
            <el-option label="markdown" :value="1" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="doSearch">查询</el-button>
          <el-button @click="doReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 文章表格 -->
    <el-card shadow="never" class="card">
      <div slot="header">
        <el-button type="primary" size="small" @click="add">新增</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column fixed type="index" align="center" width="48" />
        <el-table-column
          prop="title"
          label="文章标题"
          align="center"
          width="150"
        />
        <!-- 图片 -->
        <el-table-column label="展示图片" width="200" align="center">
          <template slot-scope="scope">
            <img
              :src="scope.row.coverImg"
              style="width: 100px; height: 100px"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
          align="center"
          width="300"
        />
        <el-table-column
          prop="author"
          label="文章作者"
          align="center"
          width="200"
        />

        <!-- switch按钮 -->
        <el-table-column label="文章是否展示" width="200" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isShow"
              :active-value="1"
              :inactive-value="0"
              @change="changeShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="editorType"
          label="内容"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.editorType === 0">富文本框</el-tag>
            <el-tag v-else-if="scope.row.editorType === 1">Markdown</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" @click="goEdit(scope.row.id)" size="small">
              <i
                style="margin-right: 6px"
                class="el-icon-view"
              />编辑文章</el-button
            >
            <el-button
              type="text"
              style="color: red"
              size="small"
              @click="del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>
  </div>
</template>

<script>
import mixin from '@/mixins/index'
import articleApi from '@/api/content/article'
// import article from '@/api/content/article'
export default {
  mixins: [mixin],
  data() {
    return {
      form: {
        author: '',
        title: '',
        editorType: ''
      },
      tableData: [],
      searchForm: {},
      loading: false,
      text: '富文本框'
    }
  },
  created() {
    this.getArticleList()
  },
  methods: {
    getArticleList() {
      this.loading = true
      setTimeout(() => {
        articleApi
          .articleListApi(
            this.page.currentPage,
            this.page.pageSize,
            this.searchForm
          )
          .then((res) => {
            if (res.success) {
              this.tableData = res.data.rows
              this.page.total = res.data.total
              console.log(this.tableData)
            } else {
              this.$message.error('获取列表失败')
            }
            this.loading = false
          })
      }, 1000)
      console.log(this.searchForm);
    },
    add() {
      this.$router.push({ path: '/content/addArticle' })
    },
    goEdit(id) {
      this.$router.push({ name: 'EditArticle', params: { id } })
    },
    //修改文章状态
    changeShow(val) {
      console.log(val)
      var temp = val.isShow === 1 ? 0 : 1
      articleApi
        .changeShowApi({
          id: val.id,
          isShow: Number(val.isShow)
        })
        .then((res) => {
          if (res.success) {
            this.$message.success('状态修改成功')
          } else {
            val.isShow = temp
            this.$message.error('状态修改失败,请检查接口')
          }
        })
    },
    doSearch() {
      this.page = this.$options.data().page
      for (const key in this.form) {
      console.log(this.form);

        // if (this.form[key] ||this.searchForm[key]  === "") {
          this.searchForm[key] = this.form[key]
        // }
      }
      this.getArticleList()
    },
    doReset() {
      // this.$options.data()的意思就是,获取到实例初始时候的数据
      this.form = this.$options.data().form
    },
    //删除文章
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          articleApi.delArticle(row.id).then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
            } else {
              this.$message.error('删除失败,请检查接口')
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getArticleList()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getArticleList()
    }
  }
}
</script>

<style scoped lang="scss">
.main {
  .card {
    margin: 30px;
  }
}
</style>

